﻿using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace Pay.Api {
    public static class WebApiConfig {
		public static void Register(HttpConfiguration config) {
			// Web API configuration and services
			var json = config.Formatters.JsonFormatter;
			// 解决json序列化时的循环引用问题
			json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
			json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
			//json.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
			//格式化时间
			var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
			json.SerializerSettings.Converters.Add(timeConverter);
			//将NULL转为""
			//config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new NullToEmptyStringResolver();
			// 干掉XML序列化器
			config.Formatters.Remove(config.Formatters.XmlFormatter);
			// Web API 配置和服务
			config.MessageHandlers.Add(new MessageDelegatingHandler());

			// Web API routes
			config.MapHttpAttributeRoutes();

			config.Routes.MapHttpRoute(
				name: "DefaultApi",
				routeTemplate: "api/{controller}/{id}",
				defaults: new { id = RouteParameter.Optional }
			);
		}
	}
}
